package com.cwt_taxi_xa.custom_view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.cwt_taxi_xa.R;


/**
 * Created by undi on 2019/7/13 11:09
 */
public class MyToggleView extends View {

    int widthSize;
    int heightSize;
    int rsSrcId;
    int rsIndicatorId;

    boolean on = false;

    public MyToggleView(Context context) {
        this(context,null);
    }

    public MyToggleView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public MyToggleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        typedYView(context,attrs, defStyleAttr);
    }

    private void typedYView(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.toggle_styleable);
        rsIndicatorId = a.getResourceId(R.styleable.toggle_styleable_viewIndicator, 0);
        rsSrcId = a.getResourceId(R.styleable.toggle_styleable_viewToggleSrc, 0);
        a.recycle();//必须回收
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
        //setMeasuredDimension(widthMeasureSpec, height);
        int w = 0;
        int h = 0;
        w = Math.max(w, getSuggestedMinimumWidth());
        h = Math.max(h, getSuggestedMinimumHeight());

        widthSize = resolveSizeAndState(w, widthMeasureSpec, 0);
        heightSize = resolveSizeAndState(h, heightMeasureSpec, 0);

        setMeasuredDimension(widthSize, heightSize);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(rsIndicatorId > 0){
            Bitmap bitmapIndicator = BitmapFactory.decodeResource(getResources(), rsIndicatorId);
            Log.d("ToggleView","bitmapIndicator  width:" + bitmapIndicator.getWidth() + " height:" + bitmapIndicator.getHeight());

            if(bitmapIndicator.getHeight() == 6){
                canvas.drawBitmap(bitmapIndicator,(widthSize - bitmapIndicator.getWidth())/2,20,null);
            }else{
                canvas.drawBitmap(bitmapIndicator,(widthSize - bitmapIndicator.getWidth())/2,9,null);
            }

        }

        if(rsSrcId > 0){
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), rsSrcId);
            Log.d("ToggleView","bitmap  width:" + bitmap.getWidth() + " height:" + bitmap.getHeight());
            if(bitmap.getHeight() == 62){
                canvas.drawBitmap(bitmap,(widthSize - bitmap.getWidth())/2,28,null);
            }if(bitmap.getHeight() == 30){
                canvas.drawBitmap(bitmap,(widthSize - bitmap.getWidth())/2,45,null);
            }else{
                int height = bitmap.getHeight()/2;
                int top = 10;
                if(bitmap.getHeight() == 154){
                    //height= 54;
                    //++top;
                }
                Bitmap newBitmap = zoomImg(bitmap,bitmap.getWidth()/2,height);
                //canvas.drawBitmap(newBitmap,(widthSize - newBitmap.getWidth())/2,(heightSize- newBitmap.getHeight())/2,null);
                canvas.drawBitmap(bitmap,(widthSize - bitmap.getWidth())/2,(heightSize- bitmap.getHeight())/2,null);
            }
        }

//        01-02 09:26:16.990 22775-22775/com.cwt_taxi_xa D/ToggleView: bitmap  width:104 height:154
//        01-02 09:26:17.000 22775-22775/com.cwt_taxi_xa D/ToggleView: bitmap  width:104 height:108
    }

    public static Bitmap zoomImg(Bitmap bm, int newWidth, int newHeight) {
        // 获得图片的宽高
        int width = bm.getWidth();
        int height = bm.getHeight();
        // 计算缩放比例
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
        Bitmap newBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
        return newBitmap;
    }

//            01-02 09:05:07.790 15028-15028/com.cwt_taxi_xa D/ToggleView: bitmapIndicator  width:39 height:6
//            01-02 09:05:07.790 15028-15028/com.cwt_taxi_xa D/ToggleView: bitmap  width:80 height:30
//            01-02 09:05:07.800 15028-15028/com.cwt_taxi_xa D/ToggleView: bitmapIndicator  width:59 height:28
//            01-02 09:05:07.800 15028-15028/com.cwt_taxi_xa D/ToggleView: bitmap  width:115 height:62

    public void setSrc(int rsSrcId){
        this.rsSrcId = rsSrcId;
        invalidate();
    }


    public void setSrcAndIndicator(int rsSrcId,int rsIndicatorId){
        this.rsSrcId = rsSrcId;
        this.rsIndicatorId = rsIndicatorId;
        invalidate();
    }

    private void setIndicator(int rsIndicatorId){
        this.rsIndicatorId = rsIndicatorId;
        invalidate();
    }
}
